using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Reflection;

namespace PlanetWPF.Toolbelt.Controls
{
    /// <summary>
    /// Represents a single row within a <see cref="PowerGrid"/>.
    /// </summary>
    public class PowerGridRow : Control
    {
        protected VirtualizingStackPanel _Presenter;

        #region Static Constructor
        static PowerGridRow()
        {
            ColumnsProperty = DependencyProperty.Register(
                "Columns",
                typeof(PowerGridColumnCollection),
                typeof(PowerGridRow),
                new FrameworkPropertyMetadata(
                  null,
                  ColumnsChanged));
            ItemProperty = DependencyProperty.Register("Item", typeof(Object), typeof(PowerGridRow), new FrameworkPropertyMetadata(null, ItemChanged));
            ParentGridProperty = DependencyProperty.Register("ParentGrid", typeof(PowerGrid), typeof(PowerGridRow), new UIPropertyMetadata(null));
            SelectedCellsProperty = DependencyProperty.Register("SelectedCells", typeof(List<PowerGridCell>), typeof(PowerGridRow), new UIPropertyMetadata(null));
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PowerGridRow), new FrameworkPropertyMetadata(typeof(PowerGridRow)));
            ChildCellsProperty = DependencyProperty.Register("ChildCells", typeof(ObservableCollection<PowerGridCell>), typeof(PowerGridColumnHeaderRow), new UIPropertyMetadata(null));
        }
        #endregion

        #region Dependency Properties
        public static readonly DependencyProperty ColumnsProperty;
        public static readonly DependencyProperty ItemProperty;
        public static readonly DependencyProperty ParentGridProperty;
        public static readonly DependencyProperty SelectedCellsProperty;
        public static readonly DependencyProperty RowsProperty;
        public static readonly DependencyProperty ChildCellsProperty;
        #endregion

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            ChildCells = new ObservableCollection<PowerGridCell>();
        }

        #region Public Properties

        public PowerGridColumnCollection Columns
        {
            get { return (PowerGridColumnCollection)GetValue(ColumnsProperty); }
            set { SetValue(ColumnsProperty, value); }
        }

        public Object Item
        {
            get { return (Object)GetValue(ItemProperty); }
            set { SetValue(ItemProperty, value); }
        }

        public PowerGrid ParentGrid
        {
            get { return (PowerGrid)GetValue(ParentGridProperty); }
            set { SetValue(ParentGridProperty, value); }
        }

        public List<PowerGridCell> SelectedCells
        {
            get { return (List<PowerGridCell>)GetValue(SelectedCellsProperty); }
            set { SetValue(SelectedCellsProperty, value); }
        }

        public ObservableCollection<PowerGridCell> ChildCells
        {
            get { return (ObservableCollection<PowerGridCell>)GetValue(ChildCellsProperty); }
            set { SetValue(ChildCellsProperty, value); }
        }

        #endregion

        #region PropertyChangedEvent Handlers
        private static void ItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }

        private static void ColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PowerGridRow tmprow = d as PowerGridRow;
            if (tmprow != null)
            {
                PowerGridColumnCollection tmpCol = e.NewValue as PowerGridColumnCollection;
                if (tmpCol != null)
                {
                    tmpCol.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(tmprow.Columns_CollectionChanged);
                }
            }
        }

        private void Columns_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
        }
        #endregion

        
        public Object CopyOutSelected()
        {
            Type tmpType = Item.GetType();
            Object tmpObject = TypeDescriptor.CreateInstance(null, tmpType, null, null);
            int imgCount = 0;
            ArrayList array = new ArrayList();
            int stringCount = 0;
            foreach (PowerGridCell cell in SelectedCells)
            {
                if (cell.CellType != PowerGridColumnType.Image)
                {
                    PropertyInfo tmpProperty = tmpType.GetProperty(cell.CellPath);
                tmpProperty.SetValue(tmpObject, cell.CellValue, null);
                }
            }
            return tmpObject;
        }

        public void CellSelectedChanged(PowerGridCell changed, bool isSelected)
        {
            if (isSelected)
            {
                AddSelectedCell(changed);
            }
            else RemoveSelectedCell(changed);
        }

        private void AddSelectedCell(PowerGridCell selected)
        {
            if (SelectedCells == null)
            {
                SelectedCells = new List<PowerGridCell>();
            }
            if (!SelectedCells.Contains(selected))
            {
                SelectedCells.Add(selected);
            }
            if (SelectedCells.Count == 1)
            {
                ParentGrid.AddSelectedRow(this);
            }
        }

        private void RemoveSelectedCell(PowerGridCell selected)
        {
            if (SelectedCells == null)
            {
                SelectedCells = new List<PowerGridCell>();
            }

            if (SelectedCells.Contains(selected))
            {
                SelectedCells.Remove(selected);
            }
            if (SelectedCells.Count == 0)
            {
                ParentGrid.RemoveSelectedRow(this);
            }
        }

        public void ClearSelections()
        {
            //SelectedCells.ForEach(new Action<PowerGridCell>(delegate (PowerGridCell tmpCell){tmpCell.Selected=false;}));
            PowerGridCell[] tmpArray = new PowerGridCell[SelectedCells.Count];
            SelectedCells.CopyTo(tmpArray);
            foreach (PowerGridCell cell in tmpArray)
            {
                cell.Selected = false;
            }
        }

        internal void RemoveCell(PowerGridCell tmpgridcell)
        {
            if (tmpgridcell != null)
            {
                ChildCells.Remove(tmpgridcell);
                tmpgridcell.CellSelected -= OnChildCellSelected;
                tmpgridcell.CellDeselected -= OnChildCellDeselected;
            }
        }

        internal void AddCell(PowerGridCell tmpgridcell)
        {
            if (tmpgridcell != null)
            {
                ChildCells.Add(tmpgridcell);
                tmpgridcell.CellSelected += OnChildCellSelected;
                tmpgridcell.CellDeselected += OnChildCellDeselected;
            }
        }

        void OnChildCellSelected(object sender, RoutedEventArgs e)
        {
            AddSelectedCell(e.Source as PowerGridCell);
        }

        private void OnChildCellDeselected(object sender, RoutedEventArgs e)
        {
            RemoveSelectedCell(e.Source as PowerGridCell);
        }

    }
}



